package com.penghaisoft.microwms.producthand.web.controller.insidewarehouse;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.penghaisoft.microwms.common.Constant;
import com.penghaisoft.microwms.common.controller.BaseController;
import com.penghaisoft.microwms.common.dto.HandResp;
import com.penghaisoft.microwms.common.dto.ProStorageSnDto;
import com.penghaisoft.microwms.common.dto.Resp;
import com.penghaisoft.microwms.common.dto.TransResult;
import com.penghaisoft.microwms.common.wms.model.ProStorageSn;
import com.penghaisoft.microwms.product.service.IProStorageSnService;

@RestController
@RequestMapping(value = "/pro/lockhand")
public class ProLockHandController extends BaseController {
	@Autowired
	private IProStorageSnService proStorageSnService;
	
	@Resource
	private StringRedisTemplate stringRedisTemplate;

	@PostMapping("lockmessage")
	public HandResp lockMessage(@RequestBody ProStorageSnDto proStorageSnDto){
	    HandResp result = new HandResp();
	    String key = Constant.PRO_LOCK_PREFIX + proStorageSnDto.getBarcode();
	    if(stringRedisTemplate.hasKey(key)){
			result.setCode(Constant.FAIL);
			result.setMsg("当前条码正在处理中，请不要重复扫描！");
		}else{
			stringRedisTemplate.opsForValue().set(key, "1", Constant.PRO_HAND_LOST_MINUTES, TimeUnit.MINUTES);//写入对象，并设置失效时间
			try{
			    TransResult transResult = proStorageSnService.lockScan(proStorageSnDto);
			    result.setCode(transResult.getCode());
			    result.setList(transResult.getList());
			    result.setMsg(transResult.getMsg());
			}catch(RuntimeException e){
        		result.setCode(Constant.FAIL);
				result.setMsg(e.getMessage());
        	}finally{
        		stringRedisTemplate.delete(key);
        	}
		}
	    return result;
	}
	
	
	@PostMapping("lockconfig")
	public HandResp lockConfig(@RequestBody ProStorageSnDto proStorageSnDto){
	    HandResp result = new HandResp();
	    String key = Constant.PRO_LOCK_PREFIX + proStorageSnDto.getBarcode();
	    if(stringRedisTemplate.hasKey(key)){
			result.setCode(Constant.FAIL);
			result.setMsg("请等" + Constant.PRO_HAND_LOST_MINUTES + "分钟再操作！");
		}else{
			stringRedisTemplate.opsForValue().set(key, "1", Constant.PRO_HAND_LOST_MINUTES, TimeUnit.MINUTES);//写入对象，并设置失效时间
			try{
			    Resp resp = proStorageSnService.lockConfirm(proStorageSnDto);
			    result.setCode(resp.getCode());
			    result.setMsg(resp.getMsg());
			}catch(RuntimeException e){
        		result.setCode(Constant.FAIL);
				result.setMsg(e.getMessage());
        	}finally{
        		stringRedisTemplate.delete(key);
        	}
		}
	    return result;
	}
}
